package com.atguigu.gmall.search.api;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import com.atguigu.gmall.search.service.SkuEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RequestMapping("/es/sku/inner")
@RestController
public class SkuEsController {




    @Autowired
    SkuEsService skuEsService;

    /**
     * es中商品上架
     * @param goods
     * @return
     */
    @PostMapping("/onsale")
    public Result saveSkuToEs(@RequestBody Goods goods){
        skuEsService.save(goods);
        return Result.ok();
    }

    @PostMapping("/canclesale/{skuId}")
    public Result deleteSkuFromEs(@PathVariable("skuId") Long  skuId){

        skuEsService.deleteById(skuId);

        return Result.ok();
    }


    /**
     * 复杂的数据检索功能
     * 检索功能
     * @param searchParam
     * @return
     */
    @PostMapping("/search")
    SearchResponseVo searchGood(@RequestBody SearchParam searchParam){


        //动态根据 SearchParam 传来的条件，
        // 构建出具有以下功能的dsl
        // 查询、过滤、模糊检索、排序、分页、高亮、聚合
        SearchResponseVo responseVo = skuEsService.search(searchParam);
        return responseVo;
    }


    /**
     * 将 skuId 商品更新成新的热度值
     * @param skuId
     * @param hotScore
     * @return
     */
    @GetMapping("/hotscore/{skuId}")
    public Result updateSkuHotScore(@PathVariable("skuId") Long skuId,
                                    @RequestParam("hotScore") Long hotScore){
        skuEsService.updateSkuHotScore(skuId,hotScore);

        return Result.ok();
    }

}
